package com.pingan.pastry.web;

import com.pingan.pastry.common.util.UserUtil;
import com.pingan.pastry.entity.User;
import com.pingan.pastry.service.UserService;

import com.pingan.pastry.spi.UserSPI;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

  /*  @Autowired
    private UserSPI userSPI;*/

    @RequestMapping("/index")
    public String login() {
        return "login";
    }

    @RequestMapping("/home")
    public String home() {
        return "starter";
    }


    @RequestMapping("/login")
    public Object login(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam("email") String email, @Param("password") String password) throws Exception {
        User user = userService.findUser(email, password);
        if (user != null) {
            //记录登录信息到session 中
            HttpSession session = request.getSession();
            authorize(session, user);
            //登录成功
            return "forward:/home";
        } else {
            //登录失败
            return "forward:/loginFailed";
        }
    }

    private void authorize(HttpSession session, User user) {
        if (user.getPermissions() != null) {
            return;
        } else {
            List<String> permissions = userService.findPermission(user.getId());
            user.setPermissions(permissions);
        }
        session.setAttribute(session.getId(), user);
    }

    @RequestMapping("/loginFailed")
    @ResponseBody
    public void loginFailed() throws Exception {
        throw new Exception("account or password error");
    }

    /**
     * 登出方法
     *
     * @param
     * @return void
     * @author longcheng
     * @date 2021/4/5 10:07
     */
    @RequestMapping("/loginOut")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        request.getRequestDispatcher("/index").forward(request, response);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<User> listUser(@RequestParam("id") String id) {
        System.out.println("/list: " + id);
        User currentUser = UserUtil.getCurrentUser();
        List<User> userList = new ArrayList<>();
        userList.add(currentUser);
        return userList;
    }

    @RequestMapping("/dmpf/list")
    @ResponseBody
    public List<User> listUser1() {
        User currentUser = UserUtil.getCurrentUser();
        //List<User> userList = new ArrayList<>();
        //userList.add(currentUser);
        /*List<User> userList = userSPI.listUser("004", 15);
        return userList;*/
        return null;
    }



}
